home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / libraries / mui20dev.lha / MUI / Developer / C / Examples / Class2.c < prev    next >
C/C++ Source or Header  |  1994-02-11  |  7KB  |  295 lines

  1. #include "demo.h"
  2.  
  3.  
  4. /***************************************************************************/
  5. /* Here is the beginning of our simple new class...                        */
  6. /***************************************************************************/
  7.  
  8. /*
  9. ** This class is the same as within Class1.c except that it features
  10. ** a pen attribute.
  11. */
  12.  
  13. struct MyData
  14. {
  15.     LONG pen;
  16. };
  17.  
  18.  
  19. #define MYATTR_PEN 0x8022   /* tag value for the new attribute.            */
  20.                             /* use 0x8000 | <yourregnumber> as upper word! */
  21.  
  22.  
  23. SAVEDS ULONG mNew(struct IClass *cl,Object *obj,Msg msg)
  24. {
  25.     struct MyData *data;
  26.  
  27.     if (!(obj = (Object *)DoSuperMethodA(cl,obj,msg)))
  28.         return(0);
  29.  
  30.     data = INST_DATA(cl,obj);
  31.  
  32.     /* parse initial taglist */
  33.  
  34.     data->pen = GetTagData(MYATTR_PEN, TEXTPEN, ((struct opSet *)msg)->ops_AttrList);
  35.  
  36.     return((ULONG)obj);
  37. }
  38.  
  39.  
  40.  
  41. SAVEDS ULONG mDispose(struct IClass *cl,Object *obj,Msg msg)
  42. {
  43.     /* OM_NEW didnt allocates something, just do nothing here... */
  44.     return(DoSuperMethodA(cl,obj,msg));
  45. }
  46.  
  47.  
  48. /*
  49. ** OM_SET method, we need to see if someone changed the pen attribute.
  50. */
  51.  
  52. SAVEDS ULONG mSet(struct IClass *cl,Object *obj,Msg msg)
  53. {
  54.     struct MyData *data = INST_DATA(cl,obj);
  55.     struct TagItem *tags,*tag;
  56.  
  57.     for (tags=((struct opSet *)msg)->ops_AttrList;tag=NextTagItem(&tags);)
  58.     {
  59.         switch (tag->ti_Tag)
  60.         {
  61.             case MYATTR_PEN:
  62.                 data->pen = tag->ti_Data;         /* set the new value */
  63.                 MUI_Redraw(obj,MADF_DRAWOBJECT);  /* redraw ourselves completely */
  64.                 break;
  65.         }
  66.     }
  67.  
  68.     return(DoSuperMethodA(cl,obj,msg));
  69. }
  70.  
  71.  
  72. /*
  73. ** OM_GET method, see if someone wants to read the color.
  74. */
  75.  
  76. static ULONG mGet(struct IClass *cl,Object *obj,Msg msg)
  77. {
  78.     struct MyData *data = INST_DATA(cl,obj);
  79.     ULONG *store = ((struct opGet *)msg)->opg_Storage;
  80.  
  81.     switch (((struct opGet *)msg)->opg_AttrID)
  82.     {
  83.         case MYATTR_PEN: *store = data->pen; return(TRUE);
  84.     }
  85.  
  86.     return(DoSuperMethodA(cl,obj,msg));
  87. }
  88.  
  89.  
  90. SAVEDS ULONG mAskMinMax(struct IClass *cl,Object *obj,struct MUIP_AskMinMax *msg)
  91. {
  92.     /*
  93.     ** let our superclass first fill in what it thinks about sizes.
  94.     ** this will e.g. add the size of frame and inner spacing.
  95.     */
  96.  
  97.     DoSuperMethodA(cl,obj,msg);
  98.  
  99.     /*
  100.     ** now add the values specific to our object. note that we
  101.     ** indeed need to *add* these values, not just set them!
  102.     */
  103.  
  104.     msg->MinMaxInfo->MinWidth  += 100;
  105.     msg->MinMaxInfo->DefWidth  += 120;
  106.     msg->MinMaxInfo->MaxWidth  += 500;
  107.  
  108.     msg->MinMaxInfo->MinHeight += 40;
  109.     msg->MinMaxInfo->DefHeight += 90;
  110.     msg->MinMaxInfo->MaxHeight += 300;
  111.  
  112.     return(0);
  113. }
  114.  
  115.  
  116. /*
  117. ** Draw method is called whenever MUI feels we should render
  118. ** our object. This usually happens after layout is finished
  119. ** or when we need to refresh in a simplerefresh window.
  120. ** Note: You may only render within the rectangle
  121. **       _mleft(obj), _mtop(obj), _mwidth(obj), _mheight(obj).
  122. */
  123.  
  124. SAVEDS ULONG mDraw(struct IClass *cl,Object *obj,struct MUIP_Draw *msg)
  125. {
  126.     struct MyData *data = INST_DATA(cl,obj);
  127.     int i;
  128.  
  129.     /*
  130.     ** let our superclass draw itself first, area class would
  131.     ** e.g. draw the frame and clear the whole region. What
  132.     ** it does exactly depends on msg->flags.
  133.     */
  134.  
  135.     DoSuperMethodA(cl,obj,msg);
  136.  
  137.     /*
  138.     ** if MADF_DRAWOBJECT isn't set, we shouldn't draw anything.
  139.     ** MUI just wanted to update the frame or something like that.
  140.     */
  141.  
  142.     if (!(msg->flags & MADF_DRAWOBJECT))
  143.         return(0);
  144.  
  145.     /*
  146.     ** ok, everything ready to render...
  147.     */
  148.  
  149.     SetAPen(_rp(obj),_dri(obj)->dri_Pens[data->pen]);
  150.  
  151.     for (i=_mleft(obj);i<=_mright(obj);i+=5)
  152.     {
  153.         Move(_rp(obj),_mleft(obj),_mbottom(obj));
  154.         Draw(_rp(obj),i,_mtop(obj));
  155.         Move(_rp(obj),_mright(obj),_mbottom(obj));
  156.         Draw(_rp(obj),i,_mtop(obj));
  157.     }
  158.  
  159.     return(0);
  160. }
  161.  
  162.  
  163. /*
  164. ** Here comes the dispatcher for our custom class. We only need to
  165. ** care about MUIM_AskMinMax and MUIM_Draw in this simple case.
  166. ** Unknown/unused methods are passed to the superclass immediately.
  167. */
  168.  
  169. SAVEDS ASM ULONG MyDispatcher(REG(a0) struct IClass *cl,REG(a2) Object *obj,REG(a1) Msg msg)
  170. {
  171.     switch (msg->MethodID)
  172.     {
  173.         case OM_NEW        : return(mNew      (cl,obj,(APTR)msg));
  174.         case OM_DISPOSE    : return(mDispose  (cl,obj,(APTR)msg));
  175.         case OM_SET        : return(mSet      (cl,obj,(APTR)msg));
  176.         case OM_GET        : return(mGet      (cl,obj,(APTR)msg));
  177.         case MUIM_AskMinMax: return(mAskMinMax(cl,obj,(APTR)msg));
  178.         case MUIM_Draw     : return(mDraw     (cl,obj,(APTR)msg));
  179.     }
  180.  
  181.     return(DoSuperMethodA(cl,obj,msg));
  182. }
  183.  
  184.  
  185.  
  186. /***************************************************************************/
  187. /* Thats all there is about it. Now lets see how things are used...        */
  188. /***************************************************************************/
  189.  
  190. int main(int argc,char *argv[])
  191. {
  192.     APTR app,window,MyObj,SuperClass,cycle;
  193.     struct IClass *MyClass;
  194.     ULONG signals;
  195.     BOOL running = TRUE;
  196.     static char *penarray[] =
  197.     {
  198.         "Detailpen",
  199.         "Blockpen",
  200.         "Textpen",
  201.         "Shinepen",
  202.         "Shadowpen",
  203.         "Fillpen",
  204.         NULL
  205.     };
  206.  
  207.     init();
  208.  
  209.     /* Get a pointer to the superclass. MUI will lock this */
  210.     /* and prevent it from being flushed during you hold   */
  211.     /* the pointer. When you're done, you have to call     */
  212.     /* MUI_FreeClass() to release this lock.               */
  213.  
  214.     if (!(SuperClass=MUI_GetClass(MUIC_Area)))
  215.         fail(NULL,"Superclass for the new class not found.");
  216.  
  217.     /* create the new class */
  218.     if (!(MyClass = MakeClass(NULL,NULL,SuperClass,sizeof(struct MyData),0)))
  219.     {
  220.         MUI_FreeClass(SuperClass);
  221.         fail(NULL,"Failed to create class.");
  222.     }
  223.  
  224.     /* set the dispatcher for the new class */
  225.     MyClass->cl_Dispatcher.h_Entry    = (APTR)MyDispatcher;
  226.     MyClass->cl_Dispatcher.h_SubEntry = NULL;
  227.     MyClass->cl_Dispatcher.h_Data     = NULL;
  228.  
  229.     app = ApplicationObject,
  230.         MUIA_Application_Title      , "Class2",
  231.         MUIA_Application_Version    , "$VER: Class2 1.0 (01.12.93)",
  232.         MUIA_Application_Copyright  , "©1993, Stefan Stuntz",
  233.         MUIA_Application_Author     , "Stefan Stuntz",
  234.         MUIA_Application_Description, "Demonstrate the use of custom classes.",
  235.         MUIA_Application_Base       , "CLASS2",
  236.  
  237.         SubWindow, window = WindowObject,
  238.             MUIA_Window_Title, "Another Custom Class",
  239.             MUIA_Window_ID   , MAKE_ID('C','L','S','2'),
  240.             WindowContents, VGroup,
  241.  
  242.                 Child, cycle = KeyCycle(penarray,' '),
  243.  
  244.                 Child, MyObj = NewObject(MyClass,NULL,
  245.                     TextFrame,
  246.                     MUIA_Background, MUII_BACKGROUND,
  247.                     TAG_DONE),
  248.  
  249.                 End,
  250.  
  251.             End,
  252.         End;
  253.  
  254.     if (!app)
  255.         fail(app,"Failed to create Application.");
  256.  
  257.     DoMethod(window,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,
  258.         app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  259.  
  260.     DoMethod(cycle,MUIM_Notify,MUIA_Cycle_Active,MUIV_EveryTime,
  261.         MyObj,3,MUIM_Set,MYATTR_PEN,MUIV_TriggerValue);
  262.  
  263.     set(cycle,MUIA_Cycle_Active,TEXTPEN);
  264.  
  265. /*
  266. ** Input loop...
  267. */
  268.  
  269.     set(window,MUIA_Window_Open,TRUE);
  270.  
  271.     while (running)
  272.     {
  273.         switch (DoMethod(app,MUIM_Application_Input,&signals))
  274.         {
  275.             case MUIV_Application_ReturnID_Quit:
  276.                 running = FALSE;
  277.                 break;
  278.         }
  279.  
  280.         if (running && signals) Wait(signals);
  281.     }
  282.  
  283.     set(window,MUIA_Window_Open,FALSE);
  284.  
  285.  
  286. /*
  287. ** Shut down...
  288. */
  289.  
  290.     MUI_DisposeObject(app);      /* dispose all objects. */
  291.     FreeClass(MyClass);          /* free our custom class. */
  292.     MUI_FreeClass(SuperClass);  /* release super class pointer. */
  293.     fail(NULL,NULL);             /* exit, app is already disposed. */
  294. }
  295.